<?php

function treatUrl( $IDUrl = false ) {
    if(empty($_GET['key'])){
        $ArUrlParameter = array('Controller' => DEFAULT_CONTROLLER_INDEX, 'Action' => DEFAULT_ACTION_INDEX);
        return empty($IDUrl) ? $ArUrlParameter : ( isset($ArUrlParameter[$IDUrl]) ? $ArUrlParameter[$IDUrl] : false );
    }
    $ArUrl = explode('/', $_GET['key']);
    $ArUrlParameter = array(
        'Controller' => !empty($ArUrl[0]) ? $ArUrl[0]: DEFAULT_CONTROLLER_INDEX,
        'Action' => !empty($ArUrl[1]) ? $ArUrl[1] : DEFAULT_ACTION_INDEX
    );
    for($index = 2, $value = $index+1, $len = count($ArUrl); $index < $len; $index += 2, $value += 2) {
        $ArUrlParameter[ $ArUrl[$index] ] = !empty($ArUrl[$value]) ? $ArUrl[$value] : null;
    }
    return empty($IDUrl) ? $ArUrlParameter : ( isset($ArUrlParameter[$IDUrl]) ? $ArUrlParameter[$IDUrl] : false );
}

function getController(){
    return treatUrl('Controller');
}

function getAction(){
    return treatUrl('Action');
}

function getParams() {
    $ArUrl = treatUrl();
    unset($ArUrl['Controller']);
    unset($ArUrl['Action']);
    $ArParams = array();
    foreach($ArUrl as $ParamKey => $ParamValue) {
        $ArParams[] = array(
            'Restriction' => $ParamKey,
            'Value' => $ParamValue
        );
    }
    return $ArParams;
}

function requireTemplatesFunctions(){
    require_once DRA_VIEW_PATH . 'core/functions.php';
}

function replaceUrlFriendlyToAction($StFriendlyAction) {
    $ArSeparators = explode('-', $StFriendlyAction);
    $StAction = $ArSeparators[0];
    for($i = 1, $len = count($ArSeparators); $i<$len; $i++) {
        $StAction .= ucwords($ArSeparators[$i]);
    }
    return $StAction;
}

function redirect($StController = '', $StAction = '', $StParams = '') {
    $StUrl = DRA_URL . $StController;
    $StUrl .= !empty($StAction) ? '/' . $StAction : '';
    $StUrl .= !empty($StAction) ? '/' . $StParams : '';
    header('Location: ' . $StUrl );
}

function getPath() {
    return !empty($_GET['key']) ? $_GET['key'] : '';
}



?>
